package com.tsingteng.cosfun.widget;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.tsingteng.cosfun.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by VincentChen on 2014/12/19.
 * <p/>
 * 发现多个tab切换效果
 */
public class CommonLinePageIndicator extends RelativeLayout implements ViewPager.OnPageChangeListener {

    private final String TAG = "DiscoverTabPageIndicator";

    private List<TextView> mTextItems;

    private LinePageIndicator mLineIndicator;

    private Activity mParentActivity;

    private int[] mViewIds;

    private int mDefaultColor = getResources().getColor(R.color.home_tab_un_select);

    private int mSelectColor = getResources().getColor(R.color.home_tab_select);

    public CommonLinePageIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * @param index
     * @param s
     */
    public void updataText(int index, String s) {
        if (index < 0 || index >= mTextItems.size()) {
            return;
        }
        mTextItems.get(index).setText(s);
    }

    /**
     * set the view ids and initialize the tab page indicator.
     * NOTE: this method MUST be called in initializing, otherwise the view would not be functional.
     *
     * @param ids an array containing the ids of the line indicator (index 0 in the array)
     *            and the text items (index from 1).
     */
    public void setViewIds(int[] ids, int selectColor, int defaultcolor) {
        if (ids != null && ids.length > 1) {
            mViewIds = ids;
            this.mDefaultColor = defaultcolor;
            this.mSelectColor = selectColor;
            initViews(selectColor);
        }
    }

    public void setViewIds(int[] ids, int selectColor, int defaultcolor, int padding) {
        if (ids != null && ids.length > 1) {
            mViewIds = ids;
            this.mDefaultColor = defaultcolor;
            this.mSelectColor = selectColor;
            initViews(selectColor);
            mLineIndicator.setmLinePadding(padding);
        }
    }

    public void setViewIds(int[] ids) {
        setViewIds(ids, mSelectColor, mDefaultColor);
    }

    public void setViewPager(ViewPager pager) {
        mLineIndicator.setViewPager(pager);
        mLineIndicator.setOnPageChangeListener(this);
    }

    public void setParentActivty(Activity parentActivity) {
        mParentActivity = parentActivity;
    }

    private void initViews(int color) {
        if (mViewIds != null && mViewIds.length > 1) {
            mTextItems = new ArrayList<TextView>(mViewIds.length - 1);
            mLineIndicator = (LinePageIndicator) findViewById(mViewIds[0]);
            mLineIndicator.setmLinePadding(40);
            mLineIndicator.setmFlag(true);
            mLineIndicator.setSelectedColor(color);
            for (int i = 1; i < mViewIds.length; i++) {
                mTextItems.add((TextView) findViewById(mViewIds[i]));
            }
            this.onPageSelected(0);
        }
        bindListener();
    }


    private void bindListener() {
        if (mTextItems != null && mTextItems.size() > 0) {
            for (int i = 0; i < mTextItems.size(); i++) {
                final int currTab = i;
                mTextItems.get(currTab).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View dv) {
                        if (mLineIndicator.getCurrentPage() == currTab && myLineChangeListener != null) {
                            myLineChangeListener.onCurrentClick(currTab);
                        }
                        mLineIndicator.setCurrentItem(currTab);
                    }
                });
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int index) {
        onSelected(index);
        if (myLineChangeListener != null) {
            myLineChangeListener.onCommonLineChange(index);
        }
    }


    public void onSelected(int index) {
        for (int i = 0; i < mTextItems.size(); i++) {
            if (index == i) {
                mTextItems.get(i).setTextColor(mSelectColor);
            } else {
                mTextItems.get(i).setTextColor(mDefaultColor);
            }
        }
    }

    public void setOnMyLineChangeListener(OnCommonLineChangeListener myLineChangeListener) {
        this.myLineChangeListener = myLineChangeListener;
    }

    private OnCommonLineChangeListener myLineChangeListener;

    public interface OnCommonLineChangeListener {

        public void onCommonLineChange(int index);

        public void onCurrentClick(int index);
    }


}
